<template>
  <div class="app-container org">
    <el-card class="leftCard">
      <!-- 搜索条件盒子 -->
      <el-row>
        <el-col :span="15">
          <p class="pinformation">搜索条件</p>
        </el-col>
      </el-row>

      <el-row>
        <el-form :model="queryParams" ref="queryRef" label-width="100">
          <el-row>
            <el-col :span="9">
              <el-form-item label="仪器名称：" prop="name" label-width="80">
                <el-input  clearable maxlength="100"
                  v-model="queryParams.name"
                  placeholder="请输入仪器名称"
                  @keyup.enter="handleQuery"
                />
              </el-form-item>
            </el-col>

            <el-col :span="9">
              <el-form-item label="仪器编号：" prop="number">
                <el-input  clearable maxlength="100"
                  v-model="queryParams.number"
                  placeholder="请输入"
                  @keyup.enter="handleQuery"
                />
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-row justify="end">
                <el-button type="success" @click.enter="handleQuery"
                  >搜索</el-button
                >
                <el-button class="resetBtn" @click="resetQuery">重置</el-button>
              </el-row>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="9">
              <el-form-item label="仪器型号：" prop="code" label-width="80">
                <el-input  clearable maxlength="100"
                  v-model="queryParams.code"
                  placeholder="请输入仪器型号"
                  @keyup.enter="handleQuery"
                />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </el-row>
      <div>
        <el-row>
            <p class="pinformation">仪器设备列表</p>
            <p class="totalStyle">共 {{ total }} 条数据</p>
        </el-row>
        <el-table
          ref="mainTableRef"
          class="business"
          v-loading="loading"
          :data="instrumentTable"
          :row-class-name="tableRowClass"
          @row-click="rowClick"
          @selection-change="handleSelectionChange"
        >
          <el-table-column label="仪器名称" align="center" prop="name" />
          <el-table-column label="仪器编号" align="center" prop="number" />
          <el-table-column label="仪器型号" align="center" prop="code" />
        </el-table>
        <pagination
          v-show="total > 0"
          :total="total"
          v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize"
          @pagination="getList"
        />
      </div>
    </el-card>

    <el-card class="rightCard">
      <!-- 搜索条件区域 -->
      <el-row>
        <el-col :span="2">
          <p class="pinformation">搜索条件</p>
        </el-col>
      </el-row>
      <el-form
        label-width="120px"
        :model="searchOutinForm"
        ref="searchOutinRef"
      >
        <el-row>
          <el-col :span="7">
            <el-form-item label="使用日期：">
              <el-date-picker
                v-model="dateRange1"
                value-format="YYYY-MM-DD"
                type="daterange"
                range-separator="-"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
              />
            </el-form-item>
          </el-col>
          <el-col :span="3">
              <el-button type="success" @click="showReportTableData" :disabled="!templateId" style="margin-left: 10px;">搜索</el-button>
              <!-- <el-button class="resetBtn" @click="resetBtn">重置</el-button> -->
          </el-col>
        </el-row>
      </el-form>
      <!-- /搜索条件区域 -->

      <!-- 技术指标表格 -->
      <el-row>
          <p class="pinformation">仪器使用记录</p>
          <p class="totalStyle">共 {{ childCount }} 条数据</p>
          <el-row style="padding-bottom: 5px;margin-left:10px;" justify="end">
          <el-button link icon="download"  type="success" @click="exportDateFormPdf"
            >导出使用记录</el-button>
            <el-button link icon="printer" type="success" @click="printDateForm"
            >打印</el-button>
        </el-row>
      </el-row>

      <templateForm ref="formRef"  v-show="isShow"/>

    </el-card>
  </div>
</template>

<script setup name="instrumentUseRecord">
import { listInstrument } from "@/api/resource/instrument";
import { listKeep } from "@/api/resource/instrumentKeep";
import TemplateForm from "@/components/TemplateForm";
import {reportTableData} from "@/api/reportTable";
import { getMonthDateRange } from "@/api/common";
const { proxy } = getCurrentInstance();
const loading = ref(true);
const ids = ref([]);
const total = ref(0);
const instrumentTable = ref([]);
const highLightRow = ref(true);
const targetTableLoading = ref(false);
const dateRange = ref([]);
const dateRange1 = ref([]);
const isShow = ref(false);
const formRef = ref(null);
const searchOutinForm = ref({
  pageNum: 1,
  pageSize: 10,
  stepId: -1
});
function printDateForm(){
  formRef.value.printDateForm();
}
const params = ref();
const templateId = ref();
const childCount = ref(0);
getMonthDateRange().then(res => {
  dateRange1.value = res.data;
})
onMounted(() => {
  nextTick(() => {
    getList();
    getTableHeight();
    window.addEventListener("resize", getTableHeight);
  })
})
onUpdated(() => {
  nextTick(function () {
    getTableHeight();
  });
});

onBeforeUnmount(() => {
  window.removeEventListener("resize", getTableHeight);
});

watch(
  () => total.value,
  () => {
    if (total.value) {
      heightParams.value = [
        { tableName: "main", bottomHeight: 73 },
        { tableName: "sub1", bottomHeight: 73 },
      ];
    } else {
      heightParams.value = [
        { tableName: "main", bottomHeight: 21 },
        { tableName: "sub1", bottomHeight: 73 },
      ];
    }
    getTableHeight();
  }
);

const heightParams = ref([
  { tableName: "main", bottomHeight: 73 },
  { tableName: "sub1", bottomHeight: 73 },
]);

function getTableHeight() {
  proxy.adaptiveHeight(proxy, heightParams.value);
}

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
  },
  rules: {},
});

const { queryParams, form, rules } = toRefs(data);

/** 查询仪器档案列表 */
function getList() {
  heightBright.value = "";
  loading.value = true;
  queryParams.value.params = {
      beginTime1: dateRange.value[0],
      endTime1: dateRange.value[1],
    };
  listInstrument(queryParams.value).then(
    (response) => {
      instrumentTable.value = response.rows;
      total.value = response.total;
      loading.value = false;
    }
  );
}

// 表单重置
function reset() {
  form.value = {};
  proxy.resetForm("");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  dateRange.value = [];
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.instrumentId);
}


function rowClick(row) {
  heightBright.value = row.instrumentId;
  templateId.value = row.templateId;
  if(!templateId.value){
    isShow.value = true;
    formRef.value.showTable(null);
    childCount.value = 0;
    return;
  }
  params.value = {instrumentId:row.instrumentId,startDate:dateRange1.value[0],endDate:dateRange1.value[1]};
  showReportTableData();
}

function showReportTableData(){
  childCount.value = 0;
  if(!dateRange1.value || dateRange1.value.length != 2){
    proxy.$modal.msgError("请选择使用日期！");
    return;
  }
  params.value.startDate = dateRange1.value[0];
  params.value.endDate = dateRange1.value[1];
  reportTableData(templateId.value,params.value).then(res => {
    isShow.value = true;
    formRef.value.showTable(res.data.formData);
    childCount.value = res.data.childCount;
  }).catch(e => {
    formRef.value.showTable(null);
    childCount.value = 0;
  });
}

const heightBright = ref("")
function tableRowClass({ row }) {
  if (row) {
    if (row.instrumentId == heightBright.value) {
      return "current-row";
    }
    return "";
  }
}

function exportDateFormPdf() {
  if(dateRange1.value.length == 0){
    proxy.$modal.msgError("请选择使用时间！");
    return;
  }
  debugger;
  params.value.startDate = dateRange1.value[0];
  params.value.endDate = dateRange1.value[1];
  const queryParams = {tableId: templateId.value,"params":params.value};
  proxy.download("/flowable/task/exportReportPdf", queryParams, `仪器使用记录.pdf`);
}
</script>

<style lang="scss" scoped></style>
